package com.ziyun.job.modules.job.task.order;

import com.cloud.bus.core.EventBusPusher;
import com.cloud.bus.event.order.OrderCancelBatchEvent;
import com.ziyun.erp.modules.order.entity.OrderInfoEntity;
import com.ziyun.erp.modules.order.service.OrderInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.List;

/**
 * @author guizhao
 * @Description: 自动取消 紫云网 N 天未支付订单
 * @date 2018-09-03 10:49
 */
@Slf4j
@Component("cancelZiyunOrderTask")
public class CancelZiyunOrderTask {

    @Autowired
    private OrderInfoService orderInfoService;

    /**
     * 自动取消紫云网未支付订单
     */
    public void cancelZiyunOrder(){
        try {
            Integer day =  7;
            String promotionType = "normal";
            // 查询 N 天内未支付订单
            List<OrderInfoEntity> list = orderInfoService.selectNotPayZiyunOrderPage(day, promotionType);
            log.info("取消紫云网未支付订单---订单数量：" + list.size());
            if(list.size() > 0){
                String orderIds = orderInfoService.cancelZiyunOrder(list);
                log.info("取消紫云网未支付最终单号：" + orderIds);
                OrderCancelBatchEvent event = OrderCancelBatchEvent.builder().source(this).orderId(orderIds).build();
                EventBusPusher.syncPost(event);
            }
            log.info("紫云网未支付订单取消完成");
        }catch (Exception e){
            log.info(e.getMessage());
        }
    }

    public void cancelZiyunOrderTest(){
        String orderIds = "190103164624597,190103164757879";
        OrderCancelBatchEvent event = OrderCancelBatchEvent.builder().source(this).orderId(orderIds).build();
        EventBusPusher.syncPost(event);
    }


}
